package lwf.handler.util;


import java.util.StringTokenizer;

import net.lwf.handler.constant.IHandlerConstant;
import net.lwf.number.util.RandomNumberGenerator;

import lwf.handler.math.ArithmaticsProcessHandler;

public class DataProcessor {
	
	//operation, digits, decimal points, datarows
	public static void getSumDetail(String inputstring, ArithmaticsProcessHandler sumdetail) {
		//ArithmaticsProcessHandler sumdetail = new ArithmaticsProcessHandler();
		StringTokenizer stk = new StringTokenizer(inputstring, ",");
		int counter = 0;
		String operation = null;
		int digits = 2; //default
		int decimals = 0;
		int datarows = 2;
		
		while(stk.hasMoreTokens()) {
			String token = stk.nextToken().trim();
			counter++;
			if(counter == 1) {
				operation = token;
			} 
			else if(counter == 2) {
				if(token != null && token.length() != 0) {
					digits = Integer.parseInt(token);
				}
			}
			else if(counter == 3) {
				if(token != null && token.length() != 0) {
					decimals = Integer.parseInt(token);
				}
			}
			else if(counter == 4) {
				if(token != null && token.length() != 0) {
					datarows = Integer.parseInt(token);
				}
			}
			
		}
		sumdetail.setDecimalDigits(decimals);
		if(operation.equals(IHandlerConstant.ADDITION)) {
			setAddData(sumdetail, digits, datarows);
		}
		else if(operation.equals(IHandlerConstant.SUBTRACTION)) {
			setSubtractData(sumdetail, digits);
		}
		else {
			setMultiplyData(sumdetail, digits);
		}
		
		//return sumdetail;

	}
	
	private static void setAddData(ArithmaticsProcessHandler sumdetail, int digits, int datarows) {
		
		sumdetail.setOperation(IHandlerConstant.ADDITION);
        int decimaldigits = sumdetail.getDecimalDigits();
		if(datarows == 0) {
			datarows = 2;
		}
		double answer = 0;
		double temp = 0;
		for(int i=0; i<datarows; i++) {
			if(decimaldigits != 0) {
				temp = RandomNumberGenerator.generateDouble(digits, decimaldigits);
				sumdetail.addData(String.valueOf(temp));
			}
			else {
				temp = RandomNumberGenerator.generate(digits);
				sumdetail.addData(String.valueOf(temp));
			}
			answer = answer + temp;
		}
		sumdetail.setAnswer(String.valueOf(answer));
		
	} //setAddData
	
	private static void setSubtractData(ArithmaticsProcessHandler sumdetail, int digits) {

		sumdetail.setOperation(IHandlerConstant.SUBTRACTION);
    	double data1 = 0;
    	double data2 = 0;	
    	int decimaldigits = sumdetail.getDecimalDigits();
	    if(decimaldigits != 0) {
	    	data1 = RandomNumberGenerator.generateDouble(digits, decimaldigits);
	    	sumdetail.setData1(String.valueOf(data1));
	    	data2 = RandomNumberGenerator.generateDouble(digits, decimaldigits);
			while (data2 > data1) {
				data2 = RandomNumberGenerator.generateDouble(digits, decimaldigits);
			}
			sumdetail.setData2(String.valueOf(data2));
		}
		else {
			data1 = RandomNumberGenerator.generate(digits);
			sumdetail.setData1(String.valueOf(data1));
			data2 = RandomNumberGenerator.generate(digits);
			while (data2 > data1) {
				data2 = RandomNumberGenerator.generate(digits);
			}
			sumdetail.setData2(String.valueOf(data2));				
		}
	    
	    sumdetail.setAnswer(String.valueOf(data1 - data2));

	} //setSubtractData	

	private static void setMultiplyData(ArithmaticsProcessHandler sumdetail, int digits) {

		double data1 = 0;
		double data2 = 0;
		
		sumdetail.setOperation(IHandlerConstant.MULTIPLICATION);

    	int decimaldigits = 0; //Setting default
	    if(decimaldigits != 0) {

		}
		else {
			data1 = RandomNumberGenerator.generate(digits);
			data2 = RandomNumberGenerator.generate(1);
			sumdetail.setData1(String.valueOf(data1));
			sumdetail.setData2(String.valueOf(data2));				
		}
	    
	    sumdetail.setAnswer(String.valueOf(data2 * data1));

	} //setMultiplyData	
	
} //DataProcessor
